// pages/login/countryPicker/countryPicker.js


var util_md5 = require("../../../utils/md5");
var util = require("../../../utils/util");
var app = getApp();
import Toast from "../../../miniprogram_npm/@vant/weapp/toast/toast";
Page({
  /**
   * 页面的初始数据
   */
  data: {
    showList: true, //展示列表
    selectData: "", //搜索之后展示的数据
    indexList: "", //右侧index数据
    countryList: "", //国家数据
  },
  clickItem(e) {
    console.log(e.currentTarget.dataset);
    app.globalData.countryData = e.currentTarget.dataset;
    wx.navigateBack()
  },
  input(e) {
    var that = this;
    let selectData = [];
    let countryData = that.data.countryList;
    if (e.detail.value) {
      for (let i in countryData) {
        for (let j = 0; j < countryData[i].data.length; j++) {
          if (countryData[i].data[j].chname.includes(e.detail.value)) {
            selectData.push(countryData[i].data[j]);
          }
        }
      }
      that.setData({
        showList: false,
        selectData: selectData,
      });
    } else {
      that.setData({
        showList: true,
      });
    }
  },
  onselect(e) {
    var that = this;
    console.log(e.detail);
    Toast({ duration: "1000", message: e.detail });
  },
  sortFunciton(property) {
    return function (a, b) {
      var value1 = a[property];

      var value2 = b[property];

      if (value1 < value2) {
        return -1;
      } else if (value1 < value2) {
        return 1;
      } else {
        return 0;
      }
    };
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    wx.request({
      url:util.api+ "/area/list",
      success(res) {
        if (res.statusCode == 200) {
          console.log(res.data.data, "00000");
          let data = res.data.data;
          let countrydata = [];
          let listData = [];
          if (data.D.length == 0) {
            console.log("111");
          }
          for (let i in data) {
            if (data[i].length !== 0) {
              countrydata.push({ name: i, data: data[i] });

              listData.push(i);
              listData.sort();
            }
          }

          console.log(countrydata, "999999");
          console.log(listData, "222");
          countrydata.sort(that.sortFunciton("name"));
          that.setData({
            countryList: countrydata,
            indexList: listData,
          });
        }
      },
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
});
